const app = getApp();
Page({
	data: {
		imgSrc: '../../../images/header.jpg',
		items: [{
			name: '1',
			value: '男',
			checked: 'true',
			id: 1
		},
		{
			name: '0',
			value: '女',
			id: 2
		}
		],
		userMess: {}
	},
	onLoad: function () {
		let data = app.globalData.loginInfo.openId;
		let that = this;
		let baseUrl = app.globalData.baseUrl;
		let header = app.globalData.loginInfo.session3rd;
		console.log(header);
		wx.request({
			url: baseUrl + '/api/user/getuserinfo',
			headers: {
				'Cookie': header
			},
			data: {
				Uidentity: data
			},
			method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				if (res.data.code === 200) {
					that.setData({
						userMess: res.data.data,
						date1: res.data.data.Ubirthday
					});
					if (res.data.data.Usex == "0") {
						that.setData({
							items: [{
								name: '1',
								value: '男',
								checked: '',
								id: 1
							},
							{
								name: '0',
								value: '女',
								checked: 'true',
								id: 2
							}
							]
						});
					} else {
						that.setData({
							items: [{
								name: '1',
								value: '男',
								checked: 'true',
								id: 1
							},
							{
								name: '0',
								value: '女',
								checked: '',
								id: 2
							}
							]
						});
					}
					if (res.data.data.Uicon) {
						that.setData({
							imgSrc: baseUrl + res.data.data.Uicon
						});
					}
				}
				// success
			},
			fail: function () {
				// fail 
			},
			complete: function () {
				// complete
			}
		});
	},
	formSubmit(e) {
		var that = this;
		let baseUrl = app.globalData.baseUrl;
		let Uidentity = app.globalData.loginInfo.openId;
		let {
			Uname,
			Usignature,
			Ubirthday,
			Uemail,
			Usex,
			Uaddr,
			Uphone
		} = e.detail.value;
		wx.request({
			url: baseUrl + '/api/user/updateUserInfo',
			data: {
				Uidentity: Uidentity,
				Uname: Uname,
				Usignature: Usignature,
				Ubirthday: Ubirthday,
				Uemail: Uemail,
				Usex: Usex,
				Uaddr: Uaddr,
				Uphone: Uphone
			},
			method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				// success
				app.globalData.loginInfo.Uname = Uname;
				wx.showLoading({
					title: '修改中',
				});
				setTimeout(function () {
					wx.hideLoading();
					wx.showModal({
						title: '',
						content: '个人信息修改完成',
						success(res) {
							if (res.confirm) {
								wx.navigateBack({
									delta: 1
								});
							} else if (res.cancel) {
								wx.navigateBack({
									delta: 1
								});
							}
						}
					});

				}, 2000);

			},
			fail: function () {
				// fail
			},
			complete: function () {
				// complete
			}
		});
	},
	formReset() {
		// console.log('form发生了reset事件');
	},
	bindDateChange(e) {
		// console.log('picker发送选择改变，携带值为', e.detail.value);
		this.setData({
			date1: e.detail.value
		});
	},
	radioChange(e) {
		// console.log('radio发生change事件，携带value值为：', e.detail.value);
	},
	chooseImage() {
		let Uidentity = app.globalData.loginInfo.openId;
		var that = this;
		let baseUrl = app.globalData.baseUrl;
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				const tempFilePaths = res.tempFilePaths;
				that.setData({
					imgSrc: tempFilePaths
				});

				wx.uploadFile({
					url: baseUrl + '/api/user/uploadicon', // 仅为示例，非真实的接口地址
					filePath: tempFilePaths[0],
					name: 'Uicon',
					formData: {
						Uidentity: Uidentity,
					},
					success(res) {
						var imgsrc = JSON.parse(res.data);
						app.globalData.loginInfo.Uicon = imgsrc.data;
						app.globalData.uiconrefresh = true;
					}
				});
			}
		});
	}
});